﻿using System;
using System.Web.Mvc;
using System.Collections.Generic;

namespace Caramel.Web.Mvc.Validation
{
	public class RequiredModelValidator : FluentModelValidator
	{
		public RequiredModelValidator(ModelMetadata metadata, ControllerContext context)
			: base(metadata, context)
		{
		}

		public override IEnumerable<ModelValidationResult> Validate(object container)
		{
			if (container == null)
			{
				return new ModelValidationResult[] { };
			}

			return
				new[]
				{
					new ModelValidationResult()
					{
						MemberName = this.Metadata.DisplayName,
						Message = String.Format("{0} is required XXX.", this.Metadata.DisplayName)
					}
				};
		}

		public override bool IsRequired
		{
			get
			{
				return true;
			}
		}

		public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
		{
			return
				new[]
				{
					new ModelClientValidationRequiredRule(String.Format("{0} is required.", this.Metadata.GetDisplayName()))
				};
		}
	}
}
